<?php
if(!defined('__CORE_DIR')){
    exit("Access Denied");
}

/**
 * Created by PhpStorm.
 * 日记图片
 * User: Administrator
 * Date: 2016/10/9 0009
 * Time: 10:26
 */
class Mdl_Home_itemphoto extends Mdl_Table
{
    protected $_table = 'home_site_item_photo';
    protected $_pk = 'photo_id';
    protected $_cols = 'photo_id,item_id,site_id,photo,title,size,orderby,dateline,status';
    protected $_orderby = array('photo_id'=>'DESC');

    public function create($data, $checked=false)
    {
        if(!$checked && !$data = $this->_check_schema($data)){
            return false;
        }
        return $this->db->insert($this->_table, $data, true);
    }

    public function update($pk, $data, $checked=false)
    {
        $this->_checkpk();
        if(!$checked && !$data = $this->_check_schema($data,  $pk)){
            return false;
        }
        return $this->db->update($this->_table, $data, $this->field($this->_pk, $pk));
    }

    public function items_by_itemid($item_id, $p=1, $l=50, &$count=0)
    {
        if(!$item_id = (int)$item_id){
            return false;
        }
        return $this->items(array('item_id'=>$item_id), $this->_orderby, $p, $l, $count);
    }

    public function items_by_siteid($site_id, $p=1, $l=50, &$count=0)
    {
        if(!$site_id = (int)$site_id){
            return false;
        }
        return $this->items(array('site_id'=>$site_id), $this->_orderby, $p, $l, $count);
    }

    public function upload($item_id, $site_id, $status, $attach)
    {
        if(!UPLOAD_ERR_OK == $attach['error']){
            $this->err->add('上传文件失败',201);
            return false;
        }
        $cfg = K::$system->config->get('attach');
        $B = '/attachs/homepics/'.date('Ym/',__CFG::TIME);
        $D = $cfg['attachdir'].$B;
        if(!$F = K::M('helper/upload')->upload($attach, $D, $fname)){
            return false;
        }

        $oImg = K::M('image/gd');
        $thumbs = $size = array();
        $size['photo'] = $cfg['homepics']['photo'] ? $cfg['homepics']['photo'] : '720';
        $size['thumb'] = $cfg['homepics']['thumb'] ? $cfg['homepics']['thumb'] : '200';
        $size['small'] = $cfg['homepics']['small'] ? $cfg['homepics']['small'] : '60X60';
        $thumbs = array($size['photo']=>"{$D}/{$fname}",$size['thumb']=>"{$D}/{$fname}_thumb.jpg", $size['small']=>"{$D}/{$fname}_small.jpg");
        $oImg->thumbs($F,$thumbs);
        if($cfg['homepics']['watermark']){
            $uname = $attach['uname'] ? $attach['uname'] : 'IJH';
            $oImg->watermark("{$D}/{$fname}", $attach['uname']);
        }
        $data = array();
        $data['item_id'] = (int)$item_id;
        $data['site_id'] = (int)$site_id;
        $data['status'] = (int)$status;
        if(!$data['title'] = $attach['title']){
            $data['title'] = preg_replace("/\.(jpg|jpeg|png|gif|bmp)$/i", '', $attach['name']);
        }
        $data['title'] = K::M('content/html')->encode($data['title']);
        $data['photo'] = $B.$fname;
        $data['photo'] = K::M('content/html')->encode($data['photo']);
        $data['size'] = $attach['size'];
        $data['dateline'] = __TIME;
        if($id =$this->db->insert($this->_table, $data, true)){
            return $data;
        }
        return false;
    }

}